import hxculture.translation.DBTranslation;
import hxculture.translation.DictionaryTranslation;
import hxculture.FormatNumber;
import hxculture.FormatDate;
import hxculture.Format;
import hxculture.Culture;
import neko.db.Mysql;
//import hxculture.cultures.ImportAll;

#if php
import php.Lib;
import php.Sys;
import php.FileSystem;
#elseif neko
import neko.Lib;
#elseif cpp
import cpp.Lib;
#end

class Test {
	public static function main()
	{
		dbTranslation();
	}
	
	public static function dbTranslation()
	{
		var conn = Mysql.connect(
			{ host : "127.0.0.1",
			  port : 3306,
			  user : "root",
			  pass : "",
			  socket : null,
			  database : "test" } );
		
		var dic = new DBTranslation(conn);
		
		if (!dic.hasSchema())
			dic.createSchema();
		/*
		dic.addDomain(hxculture.languages.It.language);
		dic.addDomain(hxculture.languages.Pt.language);
		dic.addDomain(hxculture.languages.En.language);
		
		dic.addSingular("hello world", "ciao mondo", "it");
		dic.addSingular("hello world", "olá mundo", "pt");
		dic.addSingular("hello world", "hello world", "en");
		
		dic.addPlural("file", "{0} files", ["a file", "{0} files"], "en");
		dic.addPlural("file", "{0} files", ["un file", "{0} file"], "it");
		dic.addPlural("file", "{0} files", ["um ficheiro", "{0} ficheiros"], "pt");
		*/
		
		dic.domain = "it";
		
		trace(dic._("hello world"));
		trace(dic._("hello world", "pt"));
		trace(dic._("hello world", "en"));
		
		for (i in 0...3)
		{
			for (lang in ["it", "pt", "en"])
			{
				var pattern = dic.__("file", "{0} files", i, lang);
				trace(lang + ": " + StringTools.replace(pattern, "{0}", "" + i));
			}
		}
			
		conn.close();
	}
	
	public static function pluralForms()
	{
		var dic = new DictionaryTranslation();
		dic.addDomain(hxculture.languages.Ru.language);
		dic.addPlural("File", "Files", ["Файл", "Файла", "Файлов"]);
		for (i in 0...11)
			trace(i + " " + dic.__("File", "Files", i));
	}
	
#if php
	static function __init__() {
		var dir = Sys.getCwd() + "lib/hxculture/cultures";
		for(file in FileSystem.readDirectory(dir))
			if(file != '.' && file != '..')
				untyped __call__("require_once", dir + "/" + file);
	}
#end

	static function print(s : String) {
#if flash
		trace(s);
#else
		Lib.print(s + "\n");
#end
	}

	public static function cultureFormats() {
		for(name in Culture.names()) {
			var culture = Culture.get(name);
			var pattern = "
{0}
-----------------------------------
(C)  Currency: . . . . . . . . {1:C}
(C)  Currency(2, USD): . . . . {2:C,4,'USD'}
(D)  Decimal:. . . . . . . . . {1:D}
     (default):. . . . . . . . {1,1,2} (default = 'G')
(P)  Percent:. . . . . . . . . {2:P}
(M)  Permille: . . . . . . . . {2:M}
(I)  Integer:. . . . . . . . . {1:I}
(I)  Integer:. . . . . . . . . {2:I}

(DS) Short date: . . . . . . . {3:DS}
(D)  Long date:. . . . . . . . {3:D}
(TS) Short time: . . . . . . . {3:TS}
(T)  Long time:. . . . . . . . {3:T}
(DT) Date time:. . . . . . . . {3:DT}
(DST)Short date / long time: . {3:DST}
(DSTS)Short date / short time: {3:DSTS}
(DTS)Long date / short time: . {3:DTS}
(M)  Month:. . . . . . . . . . {3:M}
(MN) Month name: . . . . . . . {3:MN}
(MS) Short month name: . . . . {3:MS}
(MF) Month Day:. . . . . . . . {3:MD}
(WD) Week day: . . . . . . . . {3:WD}
(WN) Week day name:. . . . . . {3:WN}
(WS) Week day name short:. . . {3:WS}
(R)  RFC1123:. . . . . . . . . {3:R}
(S)  Sortable: . . . . . . . . {3:S}
(U)  Universal full date/time: {3:U}
(Y)  Year: . . . . . . . . . . {3:Y}
(YM) Year Month: . . . . . . . {3:YM}";
			print(Format.string(pattern, [culture.english, -123, -456.78, Date.now()], culture));
		}
	}

	public static function dateFormats() {
		var date = Date.fromString("2004-06-04 07:08:09");
//		var date = Date.fromString("2009-01-11 15:57:09");
		var cols = 20;
		print(
			  StringTools.rpad("yearMonth", ' ', cols)
			+ " | "
			+ StringTools.rpad("monthDay", ' ', cols)
			+ " | "
			+ StringTools.rpad("date", ' ', cols)
			+ " | "
			+ StringTools.rpad("dateShort", ' ', cols)
			+ " | "
			+ StringTools.rpad("dateRfc", ' ', cols)
			+ " | "
			+ StringTools.rpad("dateTime", ' ', cols)
			+ " | "
			+ StringTools.rpad("universal", ' ', cols)
			+ " | "
			+ StringTools.rpad("sortable", ' ', cols)
			+ " | "
			+ StringTools.rpad("time", ' ', cols)
			+ " | "
			+ StringTools.rpad("timeShort", ' ', cols));
		for(name in Culture.names()) {
			var culture = Culture.get(name);
			print(culture.english + " - " + culture.name+ "\n----------------------------------------------------------------------------------------------------------------------------");
			print(
				          StringTools.rpad(FormatDate.yearMonth(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.monthDay(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.date(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.dateShort(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.dateRfc(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.dateTime(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.universal(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.sortable(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.time(date, culture), ' ', cols)
				+ " | " + StringTools.rpad(FormatDate.timeShort(date, culture), ' ', cols)
			);
			print('');
		}
	}

	public static function numberFormats() {
		var values   = [0.0, 1.2, 66, 777, -978486.03, 12345.6789, -999999999.999, 12345678901234567890.1234567890, -0.00912345678901234567890];
		var cultures = [
			hxculture.cultures.ItIT.culture,
			hxculture.cultures.HeIL.culture,
			hxculture.cultures.ArAE.culture,
			hxculture.cultures.TeIN.culture,
			hxculture.cultures.ThTH.culture,
			hxculture.cultures.GuIN.culture,
		];
		var cols = 30;
		var h =
			  StringTools.rpad("value", ' ', cols)
			+ " | "
			+ StringTools.rpad("decimal", ' ', cols)
			+ " | "
			+ StringTools.rpad("decimal(3)", ' ', cols)
			+ " | "
			+ StringTools.rpad("int", ' ', cols)
			+ " | "
			+ StringTools.rpad("currency", ' ', cols)
			+ " | "
			+ StringTools.rpad("percent", ' ', cols)
			+ " | "
			+ StringTools.rpad("permille", ' ', cols);
		for(c in cultures){
			print(c.english);
			print(StringTools.rpad('', '-', cols*7+18));
			print(h);
			for(v in values) {
				print(StringTools.rpad(''+v, ' ', cols)
					+ " | "
					+ StringTools.rpad(FormatNumber.decimal(v, c), ' ', cols)
					+ " | "
					+ StringTools.rpad(FormatNumber.decimal(v, c, 3), ' ', cols)
					+ " | "
					+ StringTools.rpad(FormatNumber.int(v, c), ' ', cols)
					+ " | "
					+ StringTools.rpad(FormatNumber.currency(v, c), ' ', cols)
					+ " | "
					+ StringTools.rpad(FormatNumber.percent(v, c), ' ', cols)
					+ " | "
					+ StringTools.rpad(FormatNumber.permille(v, c), ' ', cols));
			}
			print(StringTools.rpad('', '=', cols*6+15));
		}
	}
}